

/**
 * @author Carlos
 * @version 1.0
 * @created 09-oct-2013 05:23:50 p.m.
 */
using Imaginarium.team;
using GameEngine.utils.console;
using Imaginarium.stats;
namespace Imaginarium.battleSistem
{
    public class AbilityCommand : BattleCommand
    {
        SkillDetail detSkill;

        public AbilityCommand(Character performer, Character target, SkillDetail detSkill) :
            base(performer, target)
        {
            this.type = BattleOption.BattleOptionEnum.Ability;
            this.detSkill = detSkill;
        }
        public override string ToString()
        {
            string ans="Ability: \n";
            ans += base.ToString();
            ans += "Skill :" + detSkill.skill.name + " Lvl: " + detSkill.skillLevel +"\n";
            return ans;
        }
        public override void execute()
        {

            Performer.mp -= detSkill.calcMPCost();
            
            //Hecho a las apuradas
            Target.receiveDamage(); //only prints
            DamageMaker damageMaker = getFinalDamage();
            damageMaker.doDamage();
            damagesDealt.Add(damageMaker);

            base.execute();
        }


        public override void startAnimation()
        {
            base.startAnimation();
            Performer.setAnimation(CodeParameters.MagicAttackAnimationName);
        }

        private DamageMaker getFinalDamage()
        {
            BaseDamage skillBaseDmg = detSkill.calcBaseDamage();

            DamageMaker bd = new DamageMaker();
            bd.Target = Target;
            bd.isMagic = skillBaseDmg.isMagic;
            bd.normalDamage = System.Math.Max(
                skillBaseDmg.getNonElementalBaseDmg() * ((Performer.getFinalStats().mattk + 10) / 10) - (skillBaseDmg.isMagic ? Target.getFinalStats().mdef : Target.getFinalStats().def)
                ,0);
            if (!skillBaseDmg.hasElementalDmg())
                return bd;

            if (Target.getFinalStats().defElemental == null) Target.stats.defElemental = new Elemental();
            if (skillBaseDmg.elementalDamage.isPercentage)
            {
                bd.earthDamage += (int)System.Math.Max((skillBaseDmg.elementalDamage.earth * ( skillBaseDmg.damage * (Performer.getFinalStats().mattk + 10) / 10f) - Target.getFinalStats().mdef) * (1 - Target.getFinalStats().defElemental.earth), 0);
                bd.fireDamage += (int)System.Math.Max((skillBaseDmg.elementalDamage.fire * ( skillBaseDmg.damage * (Performer.getFinalStats().mattk + 10) / 10f) - Target.getFinalStats().mdef) * (1 - Target.getFinalStats().defElemental.fire), 0);
                bd.waterDamage += (int)System.Math.Max((skillBaseDmg.elementalDamage.water * ( skillBaseDmg.damage * (Performer.getFinalStats().mattk + 10) / 10f) - Target.getFinalStats().mdef) * (1 - Target.getFinalStats().defElemental.water), 0);
                bd.windDamage += (int)System.Math.Max((skillBaseDmg.elementalDamage.wind * (skillBaseDmg.damage * (Performer.getFinalStats().mattk + 10) / 10f) - Target.getFinalStats().mdef) * (1 - Target.getFinalStats().defElemental.wind), 0);
            }
            else
            {
                //HACER
                bd.earthDamage += (int)((Performer.getFinalStats().attk * Performer.getFinalStats().attElemental.earth / 100) * (StatsParameter.INITIAL_ELEMENTAL_DAMAGE + (Target.getFinalStats().defElemental.earth * -1)));
                bd.fireDamage += (int)((Performer.getFinalStats().attk * Performer.getFinalStats().attElemental.fire / 100) * (StatsParameter.INITIAL_ELEMENTAL_DAMAGE + (Target.getFinalStats().defElemental.fire * -1)));
                bd.waterDamage += (int)((Performer.getFinalStats().attk * Performer.getFinalStats().attElemental.water / 100) * (StatsParameter.INITIAL_ELEMENTAL_DAMAGE + (Target.getFinalStats().defElemental.water * -1)));
                bd.windDamage += (int)((Performer.getFinalStats().attk * Performer.getFinalStats().attElemental.wind / 100) * (StatsParameter.INITIAL_ELEMENTAL_DAMAGE + (Target.getFinalStats().defElemental.wind * -1)));
            }
            return bd;
        }
    }
}